﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 256 种tile 结构  2*2*2 结构的Block 中的Tile选择
/// 1 0 有没有 表示这个位置是否可以连接 
/// 占用了哪些格子 但是没有说 可以连接哪些格子？
/// 
/// x Z 视角来看
/// </summary>
using UnityEditor;

[ExecuteInEditMode]
public class TileTag : MonoBehaviour
{
    public string pos = "00000000";//8位长度序号
    public int val;
    //[ButtonCallFunc]
    //public bool valToP;
    //public void valToPMethod()
    //{

    //}
    private static List<Vector3> posList = new List<Vector3>() {
            new Vector3(-1, -1, 1),
            new Vector3(1, -1, 1),
            new Vector3(-1, 1, 1),
            new Vector3(1, 1, 1),
            new Vector3(-1, -1, -1),
            new Vector3(1, -1, -1),
            new Vector3(-1, 1, -1),
            new Vector3(1, 1, -1),
        };
    public HashSet<int> markIndex = new HashSet<int>();
    //[ButtonCallFunc]
    //public bool DrawCube;
    //public void DrawCubeMethod()
    //{
    //    foreach (var id in markIndex)
    //    {
    //        var c = GameObject.CreatePrimitive(PrimitiveType.Cube);
    //        c.transform.parent = transform;
    //        c.transform.localScale = Vector3.one * 0.5f;
    //        c.transform.localPosition = posList[id] * 0.5f;
    //    }
    //}
    private void Awake()
    {
      
    }

    [ButtonCallFunc]
    public bool InitCube;
    public void InitCubeMethod()
    {
        var go = GameObject.CreatePrimitive(PrimitiveType.Cube);
        go.transform.parent = transform;
        go.transform.localPosition = Vector3.zero;
        go.transform.localRotation = Quaternion.identity;
        go.transform.localScale = Vector3.one;
        var mr = go.GetComponent<MeshRenderer>();
        mr.sharedMaterial = AssetDatabase.LoadAssetAtPath<Material>("Assets/Material/test.mat");
        go.layer = LayerMask.NameToLayer("Click");
    }

    private void OnEnable()
    {
        if (!Application.isEditor) return;
        SceneView.onSceneGUIDelegate += OnScene;
        for (var i = 0; i < pos.Length; i++)
        {
            var c = pos[i];
            if (c == '1')
            {
                markIndex.Add(i);
            }
        }
    }
    private void OnDisable()
    {
        if (!Application.isEditor) return;
        SceneView.onSceneGUIDelegate -= OnScene;
    }
    void OnScene(SceneView scene)
    {
        var e = Event.current;
        var modify = false;
        //鼠标左键点击
        if(e.type == EventType.MouseDown && e.button == 0)
        {
            var mousePos = e.mousePosition;
            var ppp = EditorGUIUtility.pixelsPerPoint;
            mousePos.y = scene.camera.pixelHeight - mousePos.y * ppp;
            mousePos.x *= ppp;

            var ray = scene.camera.ScreenPointToRay(mousePos);
            var mask = LayerMask.GetMask("Click");
            if (Physics.Raycast(ray, out var hit, 100, mask))
            {
                var pos = hit.point;
                //Debug.Log("pos:"+pos);
                var go = hit.collider.gameObject;
                var cenPos = go.transform.position;
                var deltaPos = pos - cenPos;
                var ii = 0;
                var minIndx = 0;
                var minDist = 99999.0f;
                foreach(var p in posList)
                {
                    var dist = (p * 0.5f - deltaPos).magnitude;
                    if(dist < minDist)
                    {
                        minDist = dist;
                        minIndx = ii;
                    }
                    ii++;
                }
                if (!markIndex.Contains(minIndx))
                {
                    markIndex.Add(minIndx);
                    modify = true;
                }
            }
        }else if(e.type == EventType.MouseDown && e.button == 1)//取消
        {
            var mousePos = e.mousePosition;
            var ppp = EditorGUIUtility.pixelsPerPoint;
            mousePos.y = scene.camera.pixelHeight - mousePos.y * ppp;
            mousePos.x *= ppp;
            var ray = scene.camera.ScreenPointToRay(mousePos);
            var mask = LayerMask.GetMask("Click");
            if (Physics.Raycast(ray, out var hit, 100, mask))
            {
                var pos = hit.point;
                //Debug.Log("pos:"+pos);
                var go = hit.collider.gameObject;
                var cenPos = go.transform.position;
                var deltaPos = pos - cenPos;
                var ii = 0;
                var minIndx = 0;
                var minDist = 99999.0f;
                foreach (var p in posList)
                {
                    var dist = (p * 0.5f - deltaPos).magnitude;
                    if (dist < minDist)
                    {
                        minDist = dist;
                        minIndx = ii;
                    }
                    ii++;
                }
                //if (!markIndex.Contains(minIndx)) markIndex.Add(minIndx);
                if (markIndex.Contains(minIndx))
                {
                    markIndex.Remove(minIndx);
                    modify = true;
                }
            }
        }
        var mePos = transform.position;
        foreach (var id in markIndex)
        {
            //var c = GameObject.CreatePrimitive(PrimitiveType.Cube);
            Handles.DrawCube(id, mePos+posList[id] *0.25f, Quaternion.identity, 0.5f );
        }
        if (modify)
        {
            var list = new List<char>();
            for (var i = 0; i < 8; i++) list.Add('0');
            val = 0;
            foreach(var id in markIndex)
            {
                list[id] = '1';
                val += 1 << id;
            }
            pos = string.Join("", list);
        }
    }

}
